package com.newenergy.wisehome.ui.fragment;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.newenergy.wisehome.R;
import com.newenergy.wisehome.common.util.LocaleUtil;
import com.newenergy.wisehome.config.AppManager;
import com.newenergy.wisehome.data.bean.DeviceCategory;

import butterknife.BindView;
import butterknife.OnClick;
import me.yokeyword.fragmentation.ISupportFragment;

/**
 * Created by kyx on 2018/4/26.
 * 风扇操作页面--7202
 */

public class FanFragment extends BaseFragment {

    private static String ARG_FAN = "arg_fan";
    @BindView(R.id.btn_back)
    Button mBtnBack;
    @BindView(R.id.tv_back)
    TextView mTvBack;
    @BindView(R.id.tv_page)
    TextView mTvPage;
    @BindView(R.id.btn_switch)
    Button mBtnSwitch;
    @BindView(R.id.btn_timing)
    Button mBtnTiming;
    @BindView(R.id.btn_speed)
    Button mBtnSpeed;
    @BindView(R.id.btn_kind)
    Button mBtnKind;
    @BindView(R.id.btn_shake)
    Button mBtnShake;
    @BindView(R.id.btn_add)
    Button mBtnAdd;

    private PopupWindow mPopupWindow;

    protected DeviceCategory mDeviceCategory;


    @Override
    public int getLayoutId() {
        return R.layout.fragment_fan;
    }

    public static ISupportFragment newInstance(DeviceCategory mainItem) {
        Bundle args = new Bundle();
        args.putParcelable(ARG_FAN, mainItem);
        FanFragment fragment = new FanFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void init(View rootView) {
        mDeviceCategory = getArguments().getParcelable(ARG_FAN);
        mTvPage.setText(mDeviceCategory.getName());
        if (LocaleUtil.getLanguage().startsWith("zh")) {
            mBtnAdd.setBackgroundResource(R.drawable.btn_help_n);
            mBtnAdd.setVisibility(View.VISIBLE);
        }else {
            mBtnAdd.setVisibility(View.GONE);
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        if (mDeviceCategory != null) {
            AppManager.getInstance(getContext()).mSelectDeviceCategory = mDeviceCategory;
        }
    }

    @OnClick({R.id.btn_back, R.id.tv_back, R.id.btn_switch, R.id.btn_timing, R.id.btn_speed, R.id.btn_kind, R.id.btn_shake,R.id.btn_add})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_back:
                doBack();
                break;
            case R.id.tv_back:
                doBack();
                break;
            case R.id.btn_switch:// 开关
                AppManager.getInstance(mainActivity).commandAdvertise("01", mDeviceCategory.getFamilyId(), mDeviceCategory.getGroupId(), "01000000", "000000", mCurrFamily.getSeed(), mCurrFamily.getFeedback(), 1, "7202");

                break;
            case R.id.btn_timing://定时
                AppManager.getInstance(mainActivity).commandAdvertise("01", mDeviceCategory.getFamilyId(), mDeviceCategory.getGroupId(), "03000000", "000000", mCurrFamily.getSeed(), mCurrFamily.getFeedback(), 1, "7202");

                break;
            case R.id.btn_speed://风速
                AppManager.getInstance(mainActivity).commandAdvertise("01", mDeviceCategory.getFamilyId(), mDeviceCategory.getGroupId(), "05000000", "000000", mCurrFamily.getSeed(), mCurrFamily.getFeedback(), 1, "7202");

                break;
            case R.id.btn_kind://风类
                AppManager.getInstance(mainActivity).commandAdvertise("01", mDeviceCategory.getFamilyId(), mDeviceCategory.getGroupId(), "02000000", "000000", mCurrFamily.getSeed(), mCurrFamily.getFeedback(), 1, "7202");

                break;
            case R.id.btn_shake://摇头
                AppManager.getInstance(mainActivity).commandAdvertise("01", mDeviceCategory.getFamilyId(), mDeviceCategory.getGroupId(), "04000000", "000000", mCurrFamily.getSeed(), mCurrFamily.getFeedback(), 1, "7202");

                break;

            case R.id.btn_add:
                showPopupWindow(mBtnAdd,R.drawable.fan_speech_help_dialog);

                break;
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mDeviceCategory = null;
    }

}
